<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div slot="header" class="clearfix">
        <span>学生筛选</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="handleSearchList()">
          <i class="el-icon-search"></i>搜索
        </el-button>
      </div>
      <div class="filter-item">
        <el-input
          v-model="listQuery.keyword"
          placeholder="学生姓名/学号"
          style="width: 200px;"
          prefix-icon="el-icon-search"
          clearable
        />
        <el-select
          v-model="listQuery.classId"
          placeholder="请选择班级"
          clearable
          style="margin-left: 10px; width: 200px;"
        >
          <el-option
            v-for="item in classOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          style="margin-left: 10px;"
          @click="handleAddStudent"
        >
          添加学生
        </el-button>
      </div>
    </el-card>

    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>

    <div class="table-container">
      <el-table
        ref="studentTable"
        :data="list"
        style="width: 100%;"
        v-loading="listLoading"
        border
      >
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="学生姓名" min-width="150">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="学号" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.studentNumber }}</template>
        </el-table-column>
        <el-table-column label="所属班级" min-width="150">
          <template slot-scope="scope">{{ scope.row.className }}</template>
        </el-table-column>
        <el-table-column label="性别" width="80" align="center">
          <template slot-scope="scope">{{ scope.row.gender | genderFilter }}</template>
        </el-table-column>
        <el-table-column label="联系电话" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.phone }}</template>
        </el-table-column>
        <el-table-column label="邮箱" min-width="200">
          <template slot-scope="scope">{{ scope.row.email }}</template>
        </el-table-column>
        <el-table-column label="入学时间" width="160" align="center">
          <template slot-scope="scope">{{ scope.row.enrollmentDate }}</template>
        </el-table-column>
        <el-table-column label="状态" width="100" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status | statusFilter">
              {{ scope.row.status | statusTextFilter }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdateStudent(scope.$index, scope.row)"
            >
              编辑
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleViewCourses(scope.$index, scope.row)"
            >
              课程
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDeleteStudent(scope.$index, scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :current-page.sync="listQuery.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="listQuery.size"
        :total="total"
      />
    </div>
  </div>
</template>

<script>
import { getStudentPage, deleteStudent } from '@/api/edu'
import { getClassList } from '@/api/edu'

const defaultListQuery = {
  page: 1,
  size: 10,
  keyword: null,
  classId: null
}

export default {
  name: 'StudentList',
  filters: {
    genderFilter(gender) {
      const genderMap = {
        0: '女',
        1: '男'
      }
      return genderMap[gender]
    },
    statusFilter(status) {
      const statusMap = {
        0: 'info',
        1: 'success'
      }
      return statusMap[status]
    },
    statusTextFilter(status) {
      const statusTextMap = {
        0: '禁用',
        1: '启用'
      }
      return statusTextMap[status]
    }
  },
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: 0,
      listLoading: false,
      classOptions: []
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      // 获取班级列表
      getClassList().then(response => {
        this.classOptions = response.data
      })
      // 获取学生列表
      this.getList()
    },
    handleSearchList() {
      this.listQuery.page = 1
      this.getList()
    },
    handleAddStudent() {
      this.$router.push('/edu/student/add')
    },
    handleUpdateStudent(index, row) {
      this.$router.push(`/edu/student/update/${row.id}`)
    },
    handleViewCourses(index, row) {
      this.$router.push(`/edu/student/${row.id}/courses`)
    },
    handleDeleteStudent(index, row) {
      this.$confirm('是否要删除该学生?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteStudent(row.id).then(response => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1000
          })
          this.getList()
        })
      })
    },
    handleSizeChange(val) {
      this.listQuery.size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    getList() {
      this.listLoading = true
      getStudentPage(this.listQuery).then(response => {
        this.listLoading = false
        this.list = response.data.records
        this.total = response.data.total
      }).catch(() => {
        this.listLoading = false
      })
    }
  }
}
</script>

<style scoped>
.filter-container {
  margin-bottom: 15px;
}

.operate-container {
  margin-bottom: 15px;
}

.table-container {
  margin-bottom: 15px;
}

.pagination-container {
  display: inline-block;
  float: right;
}
</style>